package cn.javase.base.io._07_压缩和解压缩流;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;

public class _01_压缩流 {
    public static void main(String[] args) throws IOException {
        String path = "D:\\Code\\study\\JavaCode\\JavaSEDemo\\base\\src\\main\\java\\cn\\javase\\base\\io\\_07_压缩和解压缩流\\待压缩文本.txt";
        FileInputStream fileInputStream = new FileInputStream(path);
        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(new FileOutputStream(path.replace("待压缩文本.txt", "待压缩文本.txt.gz")));
        byte[] buffer = new byte[1024];
        int read;
        while ((read = fileInputStream.read(buffer)) != -1) {
            gzipOutputStream.write(buffer, 0, read);
        }
        gzipOutputStream.finish(); // 在压缩完所有数据之后调用finish()方法，以确保所有未压缩的数据都被刷新到输出流中，并生成必要的 Gzip 结束标记，标志着压缩数据的结束。
        fileInputStream.close();
        gzipOutputStream.close();
    }
}
